﻿using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Windows.Controls;

namespace wpf_print.view
{
    internal class ListView
    {
        public static Grid GetListView(List<JObject> dataList)
        {
            Grid allGrid = new();

            ColumnDefinition cdA = new();
            allGrid.ColumnDefinitions.Add(cdA);
            var allRow = 0;
            for (var i = 0; i < dataList.Count; ++i) {

                RowDefinition rdA = new();
                allGrid.RowDefinitions.Add(rdA);
                var column = (int)dataList[i]["column"];
                Grid grid = new();
                RowDefinition rd = new();
                grid.RowDefinitions.Add(rd);
                for (var j = 0; j < column; j++) {
                    ColumnDefinition cd = new();
                    grid.ColumnDefinitions.Add(cd);
                    WrapPanel warPanel;
                    if ((j + i) >= dataList.Count)
                    {
                        warPanel = new WrapPanel();
                    }
                    else {
                        warPanel = BoxView.GetBoxView(dataList[j + i]);
                    }
                    
                    Grid.SetRow(warPanel, 0);
                    Grid.SetColumn(warPanel, j);
                    grid.Children.Add(warPanel);
                }
                Grid.SetRow(grid, allRow);
                Grid.SetColumn(grid, 0);
                allGrid.Children.Add(grid);
                i += (column - 1);
                allRow++;

            };
            return allGrid;
        }
    }
}
